from datetime import datetime

from django.db import models

from apps.users.models import BaseModel
from apps.organizations.models import Teacher, CourseOrg

# 1. 设计表结构有几个重要的点
'''
一个实体对应一张表
实体1 < 关系 > 实体2
课程  章节  视频  课程资源
'''
# 2. 课程实体的具体字段

# 3. 每一个字段的类型，是否必填


class Course(BaseModel):
    # 继承底层app的基类，使字段复用性更强
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="讲师")
    course_org = models.ForeignKey(CourseOrg, null=True,  blank=True, on_delete=models.CASCADE, verbose_name="课程机构")
    name = models.CharField(verbose_name="课程名", max_length=50)
    # CharField 中max_length参数 必填
    desc = models.CharField(verbose_name="课程描述", max_length=300)
    learn_times = models.IntegerField(default=0, verbose_name="学习时长(分钟数)")
    degree = models.CharField(verbose_name="难度", choices=(("cj", "初级"), ("zj", "中级"), ("gj", "高级")), max_length=2)
    students = models.IntegerField(default=0, verbose_name="学习人数")
    fav_nums = models.IntegerField(default=0, verbose_name="收藏人数")
    # 辅助排序的字段，页面没显示。
    click_nums = models.IntegerField(default=0, verbose_name="点击数")
    # 公告字段
    notice = models.CharField(max_length=300, default="", verbose_name="课程公告")
    category = models.CharField(default=u"后端开发", max_length=20, verbose_name="课程类别")
    # 课程推荐，利用相同标签进行推荐
    tag = models.CharField(default="", verbose_name="课程标签", max_length=10)
    youneed_know = models.CharField(default="", max_length=300, verbose_name="课程须知")
    teacher_tell = models.CharField(default="", max_length=300, verbose_name="老师告诉你")
    is_classics = models.BooleanField(default=False, verbose_name="是否经典")
    detail = models.TextField(verbose_name="课程详情")
    # 以后用富文本编辑器
    image = models.ImageField(upload_to="courses/%Y/%m", verbose_name="封面图", max_length=100)
    # 是否是广告位
    is_banner = models.BooleanField(default=False, verbose_name="是否是广告位")

    def __str__(self):
        return self.name

    def lesson_nums(self):
        return self.lesson_set.all().count()

    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = verbose_name
        db_table = "tb_courses"


class CourseTag(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
    tag = models.CharField(max_length=100, verbose_name="标签")

    def __str__(self):
        return self.tag

    class Meta:
        verbose_name = "课程标签"
        verbose_name_plural = verbose_name
        db_table = "tb_coursetag"


class Lesson(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
    # on_delete表示对应的外键数据被删除后，当前的数据应该怎么办
    # CASCADE 级联 ，删除外键对应的数据，该数据也被删除，SET_NULL 设置为空，必须设置，null=True，blank=True
    name = models.CharField(max_length=100, verbose_name=u"章节名")
    learn_times = models.IntegerField(default=0, verbose_name=u"学习时长(分钟数)")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "课程章节信息"
        verbose_name_plural = verbose_name
        db_table = "tb_lessons"


class Video(BaseModel):
    lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.CASCADE)
    name = models.CharField(max_length=100, verbose_name=u"视频名")
    learn_times = models.IntegerField(default=0, verbose_name=u"学习时长(分钟数)")
    url = models.CharField(max_length=1000, verbose_name=u"访问地址")
    # 不能为空

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name
        db_table = "tb_video"


class CourseResource(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
    name = models.CharField(max_length=100, verbose_name=u"名称")
    file = models.FileField(upload_to="courses/resource/%Y/%m", verbose_name="下载地址", max_length=200)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name
        db_table = "tb_resource"
